package HCI.src;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.PushbackReader;
import java.net.Socket;

import android.app.Activity;
import android.app.Application;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.util.Printer;

public class Connector extends Application  {
	Socket socket;
	String Querry;
	 String retunval;
     String [][]Arr ;
     boolean inserted;
   //  static dataSet;
	public Connector(String Querry){
		this.Querry = Querry;
		
	}
	
	public String[][]  run() {
        try {
           // InetAddress serverAddr = InetAddress.getByName(serverIpAddress);
            Log.d("ClientActivity", "C: Connecting...");
             socket = new Socket("50.0.11.233",2944);
           // socket = new Socket("192.168.1.100",2944);

             Log.d("hello",socket.getPort()+"");
       //    while (true) {
                try {
                    Log.d("ClientActivity", "C: Sending command.");
                    PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket
                             .getOutputStream())), true);
                    out.println(Querry);
                    BufferedReader in = new BufferedReader (new InputStreamReader (socket.getInputStream()));	
                   String x = in.readLine();
                   
                  

                 
                   if(x.equals("True")){
                	   
                	   String  col = in.readLine();
                	      int colum = Integer.parseInt(col.trim());

                       String   row =in.readLine();
             	      int rows = Integer.parseInt(row.trim());

                    

                       Arr  =new String [rows][colum];
                	for(int i=0;i<Arr.length;i++){
                		 
                		for(int j=0;j<Arr[i].length;j++){
                			Log.d("fadi","helloz");
                			Arr[i][j]= in.readLine();
                			Log.d("fadi","hellozafter ");

                		}
                		
                	}
                   }
                   else{
                	   if(x.equals("False")){
                	   Arr = null;
                	   }
                   }
                   
                   if(x.equals("TrueIns")){
                	   inserted = true;
                	   
                   }
                   else{
                	   if(x.equals("FalseIns")){
                    	   inserted = false;
                    	   
                       }
                   }
               //     value = in.readLine().toString();
                  //  Log.d("plzzz",value);
                  //  Log.d("fadi",in.readLine());
             
                        // where you issue the commands
                } catch (Exception e) {
                    Log.e("ClientActivity", "S: Error", e);
                }
        //   }
       //     Log.d("ClientActivity", "C: Closed.");
            
        }
        catch (Exception e) {
        	
            Log.e("ClientActivity", "C: Error", e);
        }
        try {
        	socket.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return Arr;
    }
 
};

   
